//
//  MyShopVC.m
//  EJLX
//
//  Created by adc on 15/11/9.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "MyShopVC.h"
#import "MyShopCell.h"
#import "ReusableView.h"
#import "PersonVC.h"
#import "ShoppingOfCartVC.h"
#import "AppDelegate.h"
#import "HttpsManager.h"
#import "WDWD.h"
#import "HomeVC.h"
#import "SecondWDWD.h"

#import "ShangPinXiangQingViewController.h"
@interface MyShopVC ()

@end

@implementation MyShopVC
-(void)viewWillAppear:(BOOL)animated{
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatNavAndTabbar];

   

    [HttpsManager requestWDWD:_shopId completion:^(WDWD *me)
    {
        if (me!=nil)
        {
            _me=me;
            _latest1Ary=_me.latest1;
            _latest7Ary=_me.latest7;
            _goodsArray=_me.previous;
            __weak MyShopVC *weakSelf = self;
            [_collect addPullToRefreshWithActionHandler:^{
                NSLog(@"当进行下拉刷新执行该方法");
                [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
            }];
            
            [_collect addInfiniteScrollingWithActionHandler:^{
                [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
            }];
            [_collect reloadData];
            [self addScrollView];
        }
        else
        {
            SHOW_ALERT(@"您还没有店铺");
            [self.navigationController popViewControllerAnimated:YES];
            
        }
       
            }];
   
//    _bigscroll = [[UIScrollView alloc]initWithFrame: CGRectMake(0, 64, WIDE, HEIGHT-64-56)];
//    _bigscroll.contentSize = CGSizeMake(WIDE, 200+3*300);
//    _bigscroll.showsHorizontalScrollIndicator = NO;
//    _bigscroll.showsVerticalScrollIndicator = NO;
//    _bigscroll.backgroundColor = RGB(248, 248, 245);
//    [self.view addSubview:_bigscroll];
    
   [self addContent];
    
    [self addGoodsAndCollection];
}
- (void)requestEnd{
    [_collect.pullToRefreshView stopAnimating];
    
    [_collect.infiniteScrollingView stopAnimating];
    
}
-(void)addScrollView{
    UIView*bigView=[[UIView alloc]initWithFrame:CGRectMake(0,64,WIDE,150)];
    [self.view addSubview:bigView];
    UIImageView*bigImage=[[UIImageView alloc]initWithFrame:CGRectMake(0,0, WIDE,150)];
    NSLog(@"aaa%@",_me.brand);
    [bigImage sd_setImageWithURL:[NSURL URLWithString:_me.brand] placeholderImage:[UIImage imageNamed:@"默认背景.jpg"]];
    
    
    [bigView addSubview:bigImage];
    UIImageView*smellImage=[[UIImageView alloc]initWithFrame:CGRectMake(5,90, 80, 80)];
   [smellImage sd_setImageWithURL:[NSURL URLWithString:_me.avatar] placeholderImage:[UIImage imageNamed:@"头像"]];
    smellImage.layer.cornerRadius = 40;
    smellImage.clipsToBounds = YES;
   // smellImage.image=[UIImage  imageNamed:_me.avatar];
    [bigView addSubview:smellImage];
    UILabel*nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(65,80, 100, 30)];
   nameLabel.text=_me.shopName;
    nameLabel.font=[UIFont systemFontOfSize:12];
    [bigView addSubview:nameLabel];
    UILabel*phoneLabel=[[UILabel alloc]initWithFrame:CGRectMake(65,110, 100, 30)];
   phoneLabel.text=[NSString stringWithFormat:@"手机 %@",_me.phone];
    phoneLabel.font=[UIFont systemFontOfSize:12];
    [bigView addSubview:phoneLabel];
    
    
}
-(void)back{
  
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)creatNavAndTabbar{
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, WIDE, 44)];
    navView.backgroundColor = RGB_COLOR(0x00c49f);
    [self.view addSubview:navView];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(10,7, 30, 30);
    leftBtn.layer.cornerRadius = 20;
    leftBtn.tag = 101;
    [leftBtn setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal] ;
    [leftBtn addTarget:self action:@selector(back ) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:leftBtn];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WIDE/2-60, 5, 120, 30  )];
    title.text = @"我的微店";
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    [navView addSubview:title];
    
}
-(void)addContent{
      UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;//竖直滚动
   // layout.sectionInset = UIEdgeInsetsMake(100, 0, 20, 0);
    
        _collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0,170+64, WIDE, HEIGHT-160-64) collectionViewLayout:layout];
        _collect.dataSource = self;
        _collect.delegate = self;
  //  _collect.scrollEnabled = NO;
_collect.backgroundColor = RGB(250, 250, 250);
    _collect.alpha = 0.8;
      //  [_collect registerClass:[MyShopCell class] forCellWithReuseIdentifier:@"cell"];
    [_collect registerNib:[UINib nibWithNibName:@"MyShopCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
      //  [_collect registerClass:[ReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [_collect registerNib:[UINib nibWithNibName:@"ReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        [self.view addSubview:_collect];
    
    
}
-(void)headerImgSeletor:(UITapGestureRecognizer *)tap{
        
}
    

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section==0)
    {
        return _latest1Ary.count;
    }
    if (section==1)
    {
        return _latest7Ary.count;
    }
    return _goodsArray.count;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyShopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"MyShopCell" owner:self options:nil][0];
    }

    if (indexPath.section==0)
    {
        SecondWDWD*sec1=_latest1Ary[indexPath.row];
        [cell.goodsImageForMySop sd_setImageWithURL:[NSURL URLWithString:sec1.imgUrl] placeholderImage:[UIImage imageNamed:@"默认图片.jpg"]];
        if (sec1.discounting==NO)
        {
             cell.goodsPriceForShop.text = [NSString stringWithFormat:@"¥%.2f",sec1.price*0.01];
        }
        else
        {
            cell.goodsPriceForShop.text = [NSString stringWithFormat:@"¥%.2f",sec1.discountPrice*0.01];
        }
        
       
        cell.goodsNameForShop.text = sec1.title;
        
        cell.goodsOldPriceForShop.text = [NSString stringWithFormat:@"库存%d",sec1.stockNum];
        return cell;
    }
   
    if (indexPath.section==1)
    {
        SecondWDWD*sd=_latest7Ary[indexPath.row];
       [cell.goodsImageForMySop sd_setImageWithURL:[NSURL URLWithString:sd.imgUrl] placeholderImage:[UIImage imageNamed:@"默认图片.jpg"]];
        if (sd.discounting==NO)
        {
            cell.goodsPriceForShop.text = [NSString stringWithFormat:@"¥%.2f",sd.price*0.01];
        }
        else
        {
            cell.goodsPriceForShop.text = [NSString stringWithFormat:@"¥%.2f",sd.discountPrice*0.01];
        }
        
        
        cell.goodsNameForShop.text = sd.title;
        
        cell.goodsOldPriceForShop.text = [NSString stringWithFormat:@"库存%d",sd.stockNum];
        
        
        return cell;

    }
    SecondWDWD*last=_goodsArray[indexPath.row];
   [cell.goodsImageForMySop sd_setImageWithURL:[NSURL URLWithString:last.imgUrl] placeholderImage:[UIImage imageNamed:@"默认图片.jpg"]];
    if (last.discounting==NO)
    {
        cell.goodsPriceForShop.text = [NSString stringWithFormat:@"¥%.2f",last.price*0.01];
    }
    else
    {
        cell.goodsPriceForShop.text = [NSString stringWithFormat:@"¥%.2f",last.discountPrice*0.01];
    }
    
    
    cell.goodsNameForShop.text = last.title;
    
    cell.goodsOldPriceForShop.text = [NSString stringWithFormat:@"库存%d",last.stockNum];
    
   // cell.backgroundColor = RGB(250, 250, 250);
    return cell;
    

    
    
        }
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        SecondWDWD*me=_latest1Ary[indexPath.row];
        ShangPinXiangQingViewController*vc=[[ShangPinXiangQingViewController alloc]init];
       // vc.shopId
        vc.productId=[NSString stringWithFormat:@"%d",me.productId];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (indexPath.section==1)
    {
        SecondWDWD*me=_latest7Ary[indexPath.row];
        ShangPinXiangQingViewController*vc=[[ShangPinXiangQingViewController alloc]init];
       vc.productId=[NSString stringWithFormat:@"%d",me.productId];
        [self.navigationController pushViewController:vc animated:YES];
    }

    if (indexPath.section==2)
    {
        SecondWDWD*me=_goodsArray[indexPath.row];
        ShangPinXiangQingViewController*vc=[[ShangPinXiangQingViewController alloc]init];
         vc.productId=[NSString stringWithFormat:@"%d",me.productId];
        
        [self.navigationController pushViewController:vc animated:YES];
    }

    
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
     NSArray *titleArray = @[@"最近一天上线",@"最近7天上线",@"最近一个月"];
    ReusableView *reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    reusable.backgroundColor = [UIColor whiteColor];
    reusable.titleLB.text = titleArray[indexPath.section];
    
  
    return reusable;
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(WIDE, 44);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((WIDE-20)/2.f-2, (WIDE-20)/2.f*1.2);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    _pageControl.currentPage = scrollView.contentOffset.x;
}

-(void)addGoodsAndCollection{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
